<?php
namespace app\index\controller;

use app\common\model\banner\Banner;
use app\common\model\notice\NoticeAnnounce;
use app\common\controller\Curls;
use app\common\model\user\Users;

class Index extends Base
{
	/**
	 * 获取首页数据
	 * @return unknown
	 */
    public function index()
    {
    	//获取轮播图列表
    	$banner = Banner::where(['state' => 1])->select();
    	//获取系统公告
    	$notice = NoticeAnnounce::where(['state' => 1])->order('id desc')->field('id,title')->limit(5)->select();
    	//获取用户数据
    	$user = Users::get($this->user_id);
    	$res = [
    			'banner' => $banner,
    			'notice' => $notice,
    			'user' => $user
    	];
    	
    	return jsonSuccess($res);
    }
    
    /**
     * 获取交易行情
     * @return unknown
     */
    public function getLine()
    {
    	//获取所有交易对
    	$range = Curls::curl_post('https://api.huobi.pro/v1/common/symbols');
    	$range = json_decode($range,true)['data'];
    	//获取交易行情
    	$res = Curls::curl('https://api.huobi.pro/market/tickers');
    	$data = json_decode($res,true)['data'];
    	foreach ($data as &$rows) {
    		$rows['close'] = floatval($rows['close']);
    		$rows['open'] = floatval($rows['open']);
    		$num = (($rows['close'] - $rows['open']) / $rows['open']) * 100;
    		$num = round($num,2);
    		$rows['rate'] = $num;
    	}
    	//从大到小排序
    	$last_ages = array_column($data,'rate');
    	array_multisort($last_ages ,SORT_DESC,$data);
    	$scroll_data = [];
    	foreach ($data as &$val) {
    		foreach ($range as $v) {
    			if($val['symbol'] == $v['symbol']) {
    				$val['base_currency'] = strtoupper($v['base-currency']);
    				$val['quote_currency'] = strtoupper($v['quote-currency']);
    			}
    		}
    		if(!empty($val['base_currency']) && !empty($val['quote_currency']) && $val['quote_currency'] == 'USDT'){
    			if($val['base_currency'] == 'BTC' || $val['base_currency'] == 'ETH' || $val['base_currency'] == 'BCH')
    			$scroll_data[] = $val;
    		}
    		/* if(!empty($val['base_currency']) && !empty($val['quote_currency']) && $val['quote_currency'] == 'HUSD'){
    			if($val['base_currency'] == 'USDT')
    				$scroll_data[] = $val;
    		} */
    	}
    	$data = array_slice($data, 0,10);
//     	$scroll_data = array_slice($scroll_data, 0,10);
    	$result = [
    			'line' => $data,
    			'scroll' => $scroll_data
    	];
    	return jsonSuccess($result);
    }
}
